﻿<cfsilent>

	<cfscript>

		propAdvice = getProperty("serviceFactory").getBean("timeTablePropertyAdvice");
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		response = structNew();
		
		transaction {
			
			weekDay = event.getArg("wkday");
			lectureId = event.getArg("lec");
		
			usageAdvice = propAdvice.init();
		
			sql = "	SELECT 
						tt.props
					FROM
						t_timetable tt
					WHERE
						tt.wdidx = :weekDay 
						AND
						tt.lecidx = :lectureId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "weekDay", value = weekDay, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "lectureId", value = lectureId, cfsqltype = "cf_sql_char");
			rs_tableSetEntity = queryObj.execute(sql = sql).getResult();
			
			if (rs_tableSetEntity.recordCount eq 0) {

				sql = "	INSERT INTO 
							t_timetable(
								wdidx,
								lecidx,
								props
							)VALUES( 
								:weekDay ,
								:lectureId ,
								:props 
							)";

				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "weekDay", value = weekDay, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "lectureId", value = lectureId, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "props", value = usageAdvice.stringProperty(), cfsqltype = "cf_sql_varchar");
				queryObj.execute(sql = sql);
				
			} else {
				usageAdvice.parseProperty( rs_tableSetEntity.props );
			}
			
			switch (event.getArg('method')) {
				
				case "ProgramTask":
					/* 计划课程可用时间 */
					if (yesNoFormat(event.getArg('status'))) {
						usageAdvice.setProgramTask(true);
					}
					else {
						usageAdvice.setProgramTask(false);
					}
					break;
				
				case "CommonTask":
					/*全校通选课可用时间*/
					if (yesNoFormat(event.getArg('status'))) {
						usageAdvice.setCommonTask(true);
					}
					else {
						usageAdvice.setCommonTask(false);
					}
					break;
				
				case "OpenTask":
					/*单开班任务可用时间*/
					if (yesNoFormat(event.getArg('status'))) {
						usageAdvice.setOpenTask(true);
					}
					else {
						usageAdvice.setOpenTask(false);
					}
					break;
				
				case "SportTask":
					/*体育项目课可用时间*/
					if (yesNoFormat(event.getArg('status'))) {
						usageAdvice.setSportTask(true);
					}
					else {
						usageAdvice.setSportTask(false);
					}
					break;

			}
			
			sql = "	UPDATE
						t_timetable tt
					SET
						tt.props = :props 
					WHERE
						tt.wdidx = :weekDay 
						AND
						tt.lecidx = :lectureId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "weekDay", value = weekDay, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "lectureId", value = lectureId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "props", value = usageAdvice.stringProperty(), cfsqltype = "cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			structInsert(response, "ProgramTask", usageAdvice.getProgramTask());
			structInsert(response, "CommonTask", usageAdvice.getCommonTask());
			structInsert(response, "OpenTask", usageAdvice.getOpenTask());
			structInsert(response, "SportTask", usageAdvice.getSportTask());

		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>